library ControlAircraft initializer init requires LegacySystem

	private function ControlAircraftConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A050'
	endfunction

	private function ControlAircraft takes nothing returns nothing
		local unit u = GetSpellTargetUnit()
		local unit u2 = GetTriggerUnit()
		local player p = GetOwningPlayer(u2)
		local integer id = GetUnitTypeId(u)
		local integer id2 = GetPlayerId(GetOwningPlayer(u))
		local real x = GetUnitX(u)
		local real y = GetUnitY(u)
		if (id=='h001' or id=='hgyr' or id=='h00L') and (id2==9 or id2==11) then
			if id=='h00L' then
				call RemoveUnit(u)
				set u=CreateUnit(p,'hgyr',x,y,0.0)
			endif
			call SetUnitState(u,UNIT_STATE_MANA,100.0)
			call SetUnitOwner(u,p,false)
			call putBoolean(u,"pilot",true)
			call UnitAddAbility(u,'A00A') //+10 dmg bonus. Also used as proxy check
			call RemoveUnit(u2)
			set udg_Ogilvy = GetSpellTargetUnit()
			call StartSound(gg_snd_Rescue)
			call GeneralText(3.0, GetPlayerName(p) + " is now in control of a " + GetUnitName(u) )
			call DisplayTimedTextToPlayer(p,0.0,0.0,8.0,"Refuel and repair your aircraft at the repair bay in the airfield. Your aircraft will crash when it is out of fuel.")
		endif
		set u = null
		set u2 = null
	endfunction

	//===========================================================================
	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT  )
		call TriggerAddCondition(t,Condition( function ControlAircraftConditions ))
		call TriggerAddAction(t,function ControlAircraft)
	endfunction




endlibrary